clear all; % removes all variables, globals, functions and MEX links.
close all; % closes all the open figure windows.
clc; % clears the command window and homes the cursor.

file_name = 'moving_average_filtered_data_file_12_xrq_1';
[target] = parse_filename( file_name );

% moving_average_filtered_data
[moving_average_filtered_data_x, moving_average_filtered_data_y, moving_average_filtered_data_z, sample_time] = acceleration_acquisition(strcat('C:\Users\XRQ\Desktop\XRQ-Thesis-CAS-2016\program\segmentation1\', file_name));

% derivative
[delta_acc] = acceleration_derivative( moving_average_filtered_data_x, moving_average_filtered_data_y, moving_average_filtered_data_z );
% segmentation
[max_point, segment_point] = segmentation( delta_acc );

figure;
subplot(2, 1, 1);
plot(sample_time, moving_average_filtered_data_x, '-', sample_time, moving_average_filtered_data_y, '--', sample_time, moving_average_filtered_data_z, '-.', 'LineWidth', 2);
axis([0 2.90 -1.4 1.4]);
xlabel('Time (s)');
ylabel('Acceleration (g)');
% legend('Ax', 'Ay', 'Az');
hold on;

for i = 1 : length(segment_point)
    for j = -0.5 : 0.001 : 0.5
        plot(segment_point(i)*0.01, j, 'm');
    end
end
for i = 1 : length(max_point)
    for j = -0.5 : 0.001 : 0.5
        plot(max_point(i)*0.01, j, 'k');
    end
end


subplot(2, 1, 2);
plot(sample_time, delta_acc, 'LineWidth', 2);
axis([0 2.90 0 0.25]);
xlabel('Time (s)');
ylabel('J (g)');
hold on;

for i = 1 : length(max_point)
    plot(max_point(i)*0.01, delta_acc(max_point(i)), 'ro', 'Markersize', 10);
end
for i = 1 : length(segment_point)
    if i == 1 || i == length(segment_point)
        plot(segment_point(i)*0.01, delta_acc(segment_point(i)), 'rs', 'Markersize', 10);
    else
        plot(segment_point(i)*0.01, delta_acc(segment_point(i)), 'rv', 'Markersize', 10);
    end
end
if length(max_point) ~= 1 && length(max_point) ~= 3
    file_name
    for j = 1 : length(max_point)
        delta_acc(max_point(j))
    end
end
if length(segment_point) ~= 2 && length(segment_point) ~= 4
    file_name
    for j = 1 : length(segment_point)
        delta_acc(segment_point(j))
    end
end